package com.jinwei.qunguanjia.web.common;

import com.jinwei.qunguanjia.common.core.domain.Response;
import com.jinwei.qunguanjia.common.utils.FileUtils;
import com.jinwei.qunguanjia.common.utils.QiniuUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * 上传下载接口
 *
 */
@RestController
@RequestMapping(value = "/oss")
public class OSSFileController {

    /**
     * 获取token接口
     */
    @GetMapping("/getToken")
    public Response<String> getToken() {
        return Response.success(QiniuUtils.getToken());
    }

    /**
     * 上传接口
     * @param file 文件流
     */
    @PostMapping("/upload")
    public Response<String> upload(@RequestParam("file") MultipartFile file) throws IOException {
        String objectName = FileUtils.getStoragePath(file.getOriginalFilename());
        QiniuUtils.upload(file.getBytes(), objectName);
        return Response.success(objectName);
    }

    /**
     * 下载接口
     * @param fileName oss文件名
     */
    @GetMapping("/download")
    public Response<String> downOSSFile(String fileName) throws UnsupportedEncodingException {
        return Response.success(QiniuUtils.download(fileName));
    }
}
